<?php

namespace CbjCode\General\Library\DesignVue\src\components\form;


/**
 * Class Input
 * @property $id
 * @property $field
 * @property $suffix
 * @property $type
 * @property $value
 * @property $size
 * @property $allowClear
 * @property $disabled
 * @property $maxlength
 * @property $addonAfter
 * @property $addonBefore
 * @property $defaultValue
 */
class Input extends FormSchema
{
    /**
     * 单例私有对象
     * @var
     */
    private static $instance;

    /**
     * 组件类型
     * @var string
     */
    protected $component = 'Input';

    /**
     * 输入框的 id
     * @var string
     */
    public $id = '';



    /**
     * 带有后缀图标的 input
     * @var string|slot
     */
    public $suffix = '';

    /**
     * 输入框类型
     * @var string
     * 类型支持 请看 示例 https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/input#%3Cinput%3E_types
     */
    public $type = 'text';

    /**
     * 输入框内容
     * @var string
     */
    public $value = '';

    /**
     * 控件大小。可选 large default small
     * @var string
     */
    public $size = 'default';

    /**
     * 可以点击清除图标删除内容
     * @var bool
     */
    public $allowClear = false;

    /**
     * 是否禁用状态
     * @var bool
     */
    public $disabled = false;

    /**
     * 最大长度
     * @var number|null
     */
    public $maxlength = null;

    /**
     * 带标签的 input，设置后置标签
     * @var string|slot
     */
    public $addonAfter = '';

    /**
     * 带标签的 input，设置前置标签
     * @var string|slot
     */
    public $addonBefore = '';

    /**
     * 输入框默认内容
     * @var string
     */
    public $defaultValue = '';

    private $attrs = ['id','prefix','suffix','type','value','size','allowClear','disabled','maxlength','addonAfter','addonBefore','defaultValue'];

    public $origins = [];

    protected function __construct(array $attrs = [])
    {
        if ($attrs){
            $this->setAttrs($attrs);
        }
        $this->getOrigins();
    }

    public static function make(array $attrs = []){
        $self = self::$instance;
        if (!$self instanceof self) {
            $self = new static($attrs);
        }
        return $self->origins;
    }

    protected function getOrigins(){
        foreach ($this->attrs as $field){
            $this->origins[$field] = $this->$field;
        }
    }

    /**
     * @param array $attrs
     * @return Input|bool
     * @throws \Exception
     */
    protected function setAttrs(array $attrs = []) {
        if(empty($attrs)){
            return true;
        }
        foreach ($attrs as $field=>$value){
            if (in_array($field,$this->attrs)){
                $this->$field = $value;
            }else{
                throw new \Exception("attr {$field} 不是此Input的属性!");
            }
        }
        return $this;
    }

    public function __set($name, $value)
    {
        // TODO: Implement __set() method.
        $this->$name = $value;
    }

    public function __get($name)
    {
        // TODO: Implement __get() method.
        return $this->$name;
    }

}
